package cn.lyjuan.first.netty.demo._3_netty3.server;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Server
{
    public static void main(String[] args)
    {
        // Bootstrap
        final ServerBootstrap strap = new ServerBootstrap();

        // socker channel factory
        Executor boss = Executors.newFixedThreadPool(1);
        Executor worker = Executors.newFixedThreadPool(2);
        strap.setFactory(new NioServerSocketChannelFactory(boss, 1, worker, 2));

        // pipeline factory
        strap.setPipelineFactory(new ChannelPipelineFactory()
        {
            public ChannelPipeline getPipeline() throws Exception
            {
                ChannelPipeline pipe = Channels.pipeline();

                pipe.addLast("decoder", new StringDecoder());
                pipe.addLast("encoder", new StringEncoder());
                pipe.addLast("handler", new ServerHandler());

                return pipe;
            }
        });

        // listener
        strap.bind(new InetSocketAddress(8000));
        System.out.println("server started");
    }
}
